Page({
  data: {
    projects: [
      {
        id: 1,
        name: '生活轨迹系统开发',
        description: '开发一个记录用户日常生活轨迹的应用',
        leader: {
          id: 1,
          name: '张三',
          avatar: '/assets/icons/avatar.png'
        },
        progress: 65,
        startDate: '2024-01-15',
        endDate: '2024-06-30',
        modules: [
          {
            id: 1,
            name: '前端开发',
            leader: {
              id: 2,
              name: '李四',
              avatar: '/assets/icons/avatar.png'
            }
          },
          {
            id: 2,
            name: '后端开发',
            leader: {
              id: 3,
              name: '王五',
              avatar: '/assets/icons/avatar.png'
            }
          }
        ]
      },
      {
        id: 2,
        name: '数据分析平台',
        description: '构建企业内部数据分析和可视化平台',
        leader: {
          id: 4,
          name: '赵六',
          avatar: '/assets/icons/avatar.png'
        },
        progress: 30,
        startDate: '2024-03-01',
        endDate: '2024-08-15',
        modules: [
          {
            id: 1,
            name: '数据采集',
            leader: {
              id: 5,
              name: '钱七',
              avatar: '/assets/icons/avatar.png'
            }
          }
        ]
      }
    ],
    showProjectDialog: false,
    showModuleDialog: false,
    currentProject: null,
    currentModule: null,
    editMode: false,
    userList: [
      { id: 1, name: '张三', avatar: '/assets/icons/avatar.png' },
      { id: 2, name: '李四', avatar: '/assets/icons/avatar.png' },
      { id: 3, name: '王五', avatar: '/assets/icons/avatar.png' },
      { id: 4, name: '赵六', avatar: '/assets/icons/avatar.png' },
      { id: 5, name: '钱七', avatar: '/assets/icons/avatar.png' },
      { id: 6, name: '孙八', avatar: '/assets/icons/avatar.png' }
    ],
    newProject: {
      name: '',
      description: '',
      leader: null,
      startDate: '',
      endDate: ''
    },
    newModule: {
      name: '',
      leader: null
    }
  },

  // 展示添加项目弹窗
  showAddProjectDialog() {
    const today = new Date();
    const nextMonth = new Date(today);
    nextMonth.setMonth(today.getMonth() + 1);
    
    this.setData({
      showProjectDialog: true,
      editMode: false,
      newProject: {
        name: '',
        description: '',
        leader: null,
        startDate: this.formatDate(today),
        endDate: this.formatDate(nextMonth)
      }
    });
  },

  // 展示编辑项目弹窗
  showEditProjectDialog(e) {
    const projectId = e.currentTarget.dataset.id;
    const project = this.data.projects.find(p => p.id === projectId);
    
    if (project) {
      this.setData({
        showProjectDialog: true,
        editMode: true,
        currentProject: project,
        newProject: {
          name: project.name,
          description: project.description,
          leader: project.leader,
          startDate: project.startDate,
          endDate: project.endDate
        }
      });
    }
  },

  // 关闭项目弹窗
  closeProjectDialog() {
    this.setData({
      showProjectDialog: false
    });
  },

  // 保存项目
  saveProject() {
    const { newProject, editMode, currentProject, projects } = this.data;
    
    if (!newProject.name || !newProject.leader) {
      wx.showToast({
        title: '请填写项目名称和负责人',
        icon: 'none'
      });
      return;
    }
    
    if (editMode && currentProject) {
      // 编辑现有项目
      const updatedProjects = projects.map(p => {
        if (p.id === currentProject.id) {
          return {
            ...p,
            name: newProject.name,
            description: newProject.description,
            leader: newProject.leader,
            startDate: newProject.startDate,
            endDate: newProject.endDate
          };
        }
        return p;
      });
      
      this.setData({
        projects: updatedProjects
      });
      
      wx.showToast({
        title: '项目更新成功',
        icon: 'success'
      });
    } else {
      // 添加新项目
      const newId = projects.length > 0 ? Math.max(...projects.map(p => p.id)) + 1 : 1;
      
      const projectToAdd = {
        id: newId,
        name: newProject.name,
        description: newProject.description,
        leader: newProject.leader,
        progress: 0,
        startDate: newProject.startDate,
        endDate: newProject.endDate,
        modules: []
      };
      
      this.setData({
        projects: [...projects, projectToAdd]
      });
      
      wx.showToast({
        title: '项目创建成功',
        icon: 'success'
      });
    }
    
    this.closeProjectDialog();
  },

  // 展示添加模块弹窗
  showAddModuleDialog(e) {
    const projectId = e.currentTarget.dataset.id;
    const project = this.data.projects.find(p => p.id === projectId);
    
    if (project) {
      this.setData({
        showModuleDialog: true,
        editMode: false,
        currentProject: project,
        newModule: {
          name: '',
          leader: null
        }
      });
    }
  },

  // 展示编辑模块弹窗
  showEditModuleDialog(e) {
    const projectId = e.currentTarget.dataset.projectid;
    const moduleId = e.currentTarget.dataset.moduleid;
    
    const project = this.data.projects.find(p => p.id === projectId);
    if (project) {
      const module = project.modules.find(m => m.id === moduleId);
      if (module) {
        this.setData({
          showModuleDialog: true,
          editMode: true,
          currentProject: project,
          currentModule: module,
          newModule: {
            name: module.name,
            leader: module.leader
          }
        });
      }
    }
  },

  // 关闭模块弹窗
  closeModuleDialog() {
    this.setData({
      showModuleDialog: false
    });
  },

  // 保存模块
  saveModule() {
    const { newModule, editMode, currentProject, currentModule, projects } = this.data;
    
    if (!newModule.name || !newModule.leader) {
      wx.showToast({
        title: '请填写模块名称和负责人',
        icon: 'none'
      });
      return;
    }
    
    const updatedProjects = projects.map(p => {
      if (p.id === currentProject.id) {
        let updatedModules;
        
        if (editMode && currentModule) {
          // 编辑现有模块
          updatedModules = p.modules.map(m => {
            if (m.id === currentModule.id) {
              return {
                ...m,
                name: newModule.name,
                leader: newModule.leader
              };
            }
            return m;
          });
        } else {
          // 添加新模块
          const newId = p.modules.length > 0 ? Math.max(...p.modules.map(m => m.id)) + 1 : 1;
          
          const moduleToAdd = {
            id: newId,
            name: newModule.name,
            leader: newModule.leader
          };
          
          updatedModules = [...p.modules, moduleToAdd];
        }
        
        return {
          ...p,
          modules: updatedModules
        };
      }
      return p;
    });
    
    this.setData({
      projects: updatedProjects
    });
    
    wx.showToast({
      title: editMode ? '模块更新成功' : '模块添加成功',
      icon: 'success'
    });
    
    this.closeModuleDialog();
  },

  // 删除项目
  deleteProject(e) {
    const projectId = e.currentTarget.dataset.id;
    
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这个项目吗？此操作不可恢复。',
      success: (res) => {
        if (res.confirm) {
          this.setData({
            projects: this.data.projects.filter(p => p.id !== projectId)
          });
          
          wx.showToast({
            title: '项目已删除',
            icon: 'success'
          });
        }
      }
    });
  },

  // 删除模块
  deleteModule(e) {
    const projectId = e.currentTarget.dataset.projectid;
    const moduleId = e.currentTarget.dataset.moduleid;
    
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这个模块吗？此操作不可恢复。',
      success: (res) => {
        if (res.confirm) {
          const updatedProjects = this.data.projects.map(p => {
            if (p.id === projectId) {
              return {
                ...p,
                modules: p.modules.filter(m => m.id !== moduleId)
              };
            }
            return p;
          });
          
          this.setData({
            projects: updatedProjects
          });
          
          wx.showToast({
            title: '模块已删除',
            icon: 'success'
          });
        }
      }
    });
  },

  // 选择项目负责人
  selectProjectLeader(e) {
    const userId = e.currentTarget.dataset.id;
    const user = this.data.userList.find(u => u.id === userId);
    
    if (user) {
      this.setData({
        'newProject.leader': user
      });
    }
  },

  // 选择模块负责人
  selectModuleLeader(e) {
    const userId = e.currentTarget.dataset.id;
    const user = this.data.userList.find(u => u.id === userId);
    
    if (user) {
      this.setData({
        'newModule.leader': user
      });
    }
  },

  // 格式化日期
  formatDate(date) {
    const d = new Date(date);
    const year = d.getFullYear();
    const month = String(d.getMonth() + 1).padStart(2, '0');
    const day = String(d.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
  },

  // 开始日期变更
  onStartDateChange(e) {
    this.setData({
      'newProject.startDate': e.detail.value
    });
  },

  // 结束日期变更
  onEndDateChange(e) {
    this.setData({
      'newProject.endDate': e.detail.value
    });
  },

  // 阻止事件冒泡
  stopPropagation() {
    return;
  }
}) 